<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class User_Model_Friend extends Zend_Db_Table_Abstract {

    protected $_name = TABLE_FRIEND;
    protected $_id = 'id';

    /*
     * 0 : Chưa có gì
     * 1 : Gửi lời mời kết bạn
     * 2 : Nhận được lời mời kết bạn
     * 3 : 2 người là bạn của nhau
     */

    public function isFriend($userId, $friendId) {
        if ($userId == $friendId) {
            return 3;
        }
        $row = $this->fetchRow('user_id="' . $userId . '" and friend_id="' . $friendId . '"');
        if (!$row) {
            return 0;
        }
        return $row['status'];
    }

    public function getFriend($userId, $order = null, $count = null, $offset = null, $status=3) {

        $name = array('p' => TABLE_MEMBERS);
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('f' => $this->_name))
                ->join(array('m' => TABLE_MEMBERS), 'f.friend_id = m.id')
                ->where('f.status=' . $status .' and f.user_id=' .$userId);
//        $where = 'f.status=' . $status .' and f.user_id=' .$userId;
        
        if ($order) {
            $select->order($order);
        }
        if ($count !== null || $offset !== null) {
            $select->limit($count, $offset);
        }
        $row = $this->fetchAll($select)->toArray();
        return $row;
    }
    
    public function countFriend($userId){
        return $this->fetchAll('status=3 and user_id=' .$userId)->count();
    }

    public function addFriend($userId, $friendId, $content=null) {
        $insert1 = $this->insert(array('user_id' => $userId, 'friend_id' => $friendId, 'status' => 1));
        $insert2 = $this->insert(array('user_id' => $friendId, 'friend_id' => $userId, 'status' => 2));
        $feedModel = new User_Model_Feed();
        $feedData = array(
            'feed_type' => 3,
            'feed_content' => $content,
            'feed_create' => time(),
            'user_id' => $userId,
            'friend_id' => $friendId
        );
        $feedModel->insert($feedData);
        if ($insert1 && $insert2) {
            return TRUE;
        }
        return FALSE;
    }
    
    public function deleteFriend($userId, $friendId) {
        $update1 = $this->delete('user_id="' . $userId . '" and friend_id="' . $friendId . '"');
        $update2 = $this->delete('user_id="' . $friendId . '" and friend_id="' . $userId . '"');
        return true;
    }

    public function updateFriend($userId, $friendId) {
        $update1 = $this->update(array('status' => 3), 'user_id="' . $userId . '" and friend_id="' . $friendId . '"');
        $update2 = $this->update(array('status' => 3), 'user_id="' . $friendId . '" and friend_id="' . $userId . '"');
        $feedModel = new User_Model_Feed();
        $feedData = array(
            'feed_type' => 2,
            'feed_create' => time()
        );
        $feedModel->update($feedData, '(user_id=' . $userId . ' and friend_id=' . $friendId.') or (user_id=' . $friendId . ' and friend_id=' . $userId.')');
        if ($insert1 && $insert2) {
            return TRUE;
        }
        return FALSE;
    }

}

?>
